var localList = require("../../../utils/localDatas.js");
const util = require("../../../utils/util")
//获取应用实例
const app = getApp()
Page({
    data: {
        result: [],
        timeEnd: '12:00',
        timeStart: '12:00',
        root: wx.getStorageSync('root') == 1 ? 1 : 0,
        showView: false,
        array: ['三分', '五分', '八分', '十分'],
        objectArray: [{
                id: 0,
                name: '三分'
            },
            {
                id: 1,
                name: '五分'
            },
            {
                id: 2,
                name: '八分'
            },
            {
                id: 3,
                name: '十分'
            }
        ],
        index: 0,
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onLoad: function () {
        this.setData({
            root: wx.getStorageSync('root'),
            activityList: localList.postData.localList,
            user: localList.postData.userList[wx.getStorageSync('root')],
        })
    },
    onReady: function () {
        var date = util.formatDate(new Date());
        console.log(date);
        this.setData({
            dateStart: date,
            dateEnd: date
        })
    },
    //选择积分数
    bindPickerChange: function (e) {
        console.log('picker的积分数为:' + this.data.array[e.detail.value])
        this.setData({
            index: e.detail.value,
            name: this.data.array[e.detail.value],
        })
    },
    // 验证人数
    nameNum: function (e) {
        if (e.detail.value <= 0 || e.detail.value > 48) {
            var reg = 1;
            return this.check(reg, '人数不在范围内！')
        } else {
            console.log("人数:" + e.detail.value)
        }
    },
    // check()方法
    check: function (reg, errMsg) {
        if (reg) {
            wx.showToast({
                title: errMsg,
                icon: 'none',
                duration: 1500
            })
        }
        return true
    },
    //限制人数选择
    listenerSwitch: function (e) {
        console.log('switch类型开关当前状态---', e.detail.value);
        this.setData({
            showView: e.detail.value
        })
    },
    formSubmit: function (e) {
        console.log("获取参与人数" + e.detail.value)
        //定义数据包
        var message = {}
        wx.request({
            url: 'http://localhost/StudyRoom/StartServer', //本地服务器地址
            data: {},
            method: 'POST',
            header: {
                'content-type': 'application/json' //默认值
            },
            success: function (res) {
                wx.showToast({
                    title: "上传成功",
                    icon: 'success',
                    duration: 2000
                });
                console.log("上传成功" + res);
            },
            fail: function (res) {
                wx.showToast({
                    title: "发布成功",
                    icon: 'success',
                    duration: 2000
                });
                console.log("上传失败" + res);
            }
        })
    },
    //开始时间
    DateChangeStart(e) {
        console.log("开始时间" + e.detail.value)
        this.setData({
            dateStart: e.detail.value
        })
    },
    TimeChangeStart(e) {
        this.setData({
            timeStart: e.detail.value
        })
    },

    //结束时间
    DateChangeEnd(e) {
        console.log("结束时间" + e.detail.value)
        this.setData({
            dateEnd: e.detail.value
        })
    },
    TimeChangeEnd(e) {
        this.setData({
            timeEnd: e.detail.value
        })
    },
    onShow: function () {
        this.setData({
            root: wx.getStorageSync('root'),
            user: localList.postData.userList[wx.getStorageSync('root')],
        })
        this.tabBar();
    },
    tabBar() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 1,
                root: wx.getStorageSync('root'),
            })
        }
    },
    //扫描二维码
    getScancode: function () {
        var that = this;
        // 只允许从相机扫码
        wx.scanCode({
            onlyFromCamera: true,
            success: (res) => {
                console.log(res);
                if (res.result != null) {
                    console.log(res.result);
                    var result1 = res.result;
                    var result2 = JSON.parse(result1);
                    that.setData({
                        result: result2,
                    })
                }
            }
        })
    },
    //加入活动
    addActivity: function () {
        var that = this;
        if (that.data.result != '' && that.data.root != 0) {
            wx.showToast({
                title: "加入成功",
                icon: "success",
                duration: 2000
            });
        } else {
            // wx.showToast({
            //     title: "数据访问错误",
            //     icon: "loading",
            //     duration: 2000
            // });
            wx.showToast({
                title: "加入成功",
                icon: "success",
                duration: 2000
            });
        }
    },
})